<template>
  <div class="my-coupons-page">
    <!-- 顶部导航栏 -->
    <Navbar />

    <div class="container">
      <!-- 面包屑 -->
      <el-breadcrumb separator="/" class="breadcrumb">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>我的优惠券</el-breadcrumb-item>
      </el-breadcrumb>

      <!-- 筛选标签 -->
      <div class="filter-tabs">
        <el-radio-group v-model="statusFilter" @change="handleFilterChange">
          <el-radio-button label="">全部</el-radio-button>
          <el-radio-button label="0">未使用</el-radio-button>
          <el-radio-button label="1">已使用</el-radio-button>
          <el-radio-button label="2">已过期</el-radio-button>
        </el-radio-group>
      </div>

      <!-- 优惠券列表 -->
      <div class="coupons-content" v-loading="loading">
        <el-empty 
          v-if="!loading && couponList.length === 0" 
          description="暂无优惠券"
        >
          <el-button type="primary" @click="goToCouponCenter">去领券</el-button>
        </el-empty>

        <div v-else class="coupon-grid">
          <div 
            v-for="coupon in couponList" 
            :key="coupon.userCouponId"
            class="coupon-card"
            :class="getCouponStatusClass(coupon.status)"
          >
            <div class="coupon-left">
              <div class="coupon-amount">
                <template v-if="coupon.couponType === '0'">
                  <span class="amount-symbol">¥</span>
                  <span class="amount-value">{{ coupon.discountAmount }}</span>
                </template>
                <template v-else-if="coupon.couponType === '1'">
                  <span class="amount-value">{{ (coupon.discountRate * 10).toFixed(1) }}</span>
                  <span class="amount-unit">折</span>
                </template>
                <template v-else>
                  <span class="amount-text">免费</span>
                </template>
              </div>
              <div class="coupon-condition">
                满{{ coupon.minAmount }}可用
              </div>
            </div>

            <div class="coupon-right">
              <div class="coupon-info">
                <h3 class="coupon-name">{{ coupon.couponName }}</h3>
                <div class="coupon-meta">
                  <el-tag :type="getCouponTypeTag(coupon.couponType)" size="small">
                    {{ getCouponTypeName(coupon.couponType) }}
                  </el-tag>
                  <el-tag :type="getStatusTag(coupon.status)" size="small">
                    {{ getStatusName(coupon.status) }}
                  </el-tag>
                </div>
                <div class="coupon-time">
                  <div>
                    <el-icon><Clock /></el-icon>
                    有效期至：{{ formatTime(coupon.expireTime) }}
                  </div>
                  <div class="receive-time">
                    领取时间：{{ formatDateTime(coupon.receiveTime) }}
                  </div>
                  <div v-if="coupon.useTime" class="use-time">
                    使用时间：{{ formatDateTime(coupon.useTime) }}
                  </div>
                </div>
              </div>

              <div class="coupon-status">
                <el-tag 
                  :type="getStatusTag(coupon.status)"
                  size="large"
                  effect="plain"
                  class="status-tag"
                >
                  {{ getStatusName(coupon.status) }}
                </el-tag>
              </div>
            </div>
          </div>
        </div>

        <!-- 分页 -->
        <el-pagination
          v-if="total > 0"
          class="pagination"
          v-model:current-page="pageNum"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next"
          @size-change="loadMyCoupons"
          @current-change="loadMyCoupons"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { getMyCoupons } from '@/api/coupon'
import { ElMessage } from 'element-plus'
import { Clock } from '@element-plus/icons-vue'
import Navbar from '@/components/Navbar.vue'
import dayjs from 'dayjs'

const router = useRouter()
const userStore = useUserStore()

const loading = ref(false)
const couponList = ref([])
const statusFilter = ref('')
const pageNum = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 加载我的优惠券
const loadMyCoupons = async () => {
  loading.value = true
  try {
    const data = await getMyCoupons({
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      status: statusFilter.value || undefined
    })
    couponList.value = data.list || []
    total.value = data.total || 0
  } catch (error) {
    console.error('获取优惠券失败:', error)
  } finally {
    loading.value = false
  }
}

// 筛选变化
const handleFilterChange = () => {
  pageNum.value = 1
  loadMyCoupons()
}

// 跳转到优惠券中心
const goToCouponCenter = () => {
  router.push('/coupon-center')
}

// 获取优惠券类型名称
const getCouponTypeName = (type) => {
  const typeMap = {
    '0': '满减券',
    '1': '折扣券',
    '2': '免费券'
  }
  return typeMap[type] || '优惠券'
}

// 获取优惠券类型标签
const getCouponTypeTag = (type) => {
  const tagMap = {
    '0': 'danger',
    '1': 'warning',
    '2': 'success'
  }
  return tagMap[type] || 'info'
}

// 获取状态名称
const getStatusName = (status) => {
  const statusMap = {
    '0': '未使用',
    '1': '已使用',
    '2': '已过期'
  }
  return statusMap[status] || '未知'
}

// 获取状态标签
const getStatusTag = (status) => {
  const tagMap = {
    '0': 'success',
    '1': 'info',
    '2': 'danger'
  }
  return tagMap[status] || 'info'
}

// 获取优惠券状态样式类
const getCouponStatusClass = (status) => {
  if (status === '1' || status === '2') {
    return 'coupon-disabled'
  }
  return ''
}

// 格式化日期
const formatTime = (time) => {
  return dayjs(time).format('YYYY-MM-DD')
}

// 格式化日期时间
const formatDateTime = (time) => {
  return dayjs(time).format('YYYY-MM-DD HH:mm')
}

onMounted(() => {
  // 检查是否登录
  if (!userStore.isLoggedIn()) {
    ElMessage.warning('请先登录')
    router.push('/')
    return
  }
  loadMyCoupons()
})
</script>

<style scoped>
.my-coupons-page {
  width: 100%;
  min-height: 100vh;
  background-color: #f5f5f5;
  margin: 0;
  padding: 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.breadcrumb {
  margin-bottom: 20px;
  padding-top: 20px;
}

.filter-tabs {
  background: white;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
}

.coupons-content {
  min-height: 400px;
}

.coupon-grid {
  display: grid;
  gap: 20px;
  margin-bottom: 20px;
}

.coupon-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  display: flex;
  transition: all 0.3s;
  position: relative;
}

.coupon-card::before {
  content: '';
  position: absolute;
  left: 180px;
  top: -5px;
  bottom: -5px;
  width: 10px;
  background: radial-gradient(circle at left, transparent 5px, white 5px);
  background-size: 10px 10px;
  background-position: left center;
}

.coupon-left {
  width: 180px;
  background: linear-gradient(135deg, #ff6b6b 0%, #ff4d4f 100%);
  color: white;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 30px 20px;
}

.coupon-disabled .coupon-left {
  background: linear-gradient(135deg, #bbb 0%, #999 100%);
}

.coupon-amount {
  display: flex;
  align-items: baseline;
  margin-bottom: 10px;
}

.amount-symbol {
  font-size: 24px;
  font-weight: bold;
}

.amount-value {
  font-size: 48px;
  font-weight: bold;
  line-height: 1;
}

.amount-unit {
  font-size: 24px;
  font-weight: bold;
  margin-left: 4px;
}

.amount-text {
  font-size: 36px;
  font-weight: bold;
}

.coupon-condition {
  font-size: 14px;
  opacity: 0.9;
}

.coupon-right {
  flex: 1;
  padding: 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.coupon-info {
  flex: 1;
}

.coupon-name {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  margin: 0 0 12px 0;
}

.coupon-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

.coupon-time {
  font-size: 13px;
  color: #666;
}

.coupon-time > div {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-bottom: 5px;
}

.receive-time,
.use-time {
  color: #999;
  font-size: 12px;
}

.coupon-status {
  margin-left: 20px;
}

.status-tag {
  font-size: 16px;
  padding: 10px 20px;
}

.coupon-disabled {
  opacity: 0.6;
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 30px;
}

@media (max-width: 768px) {
  .coupon-card {
    flex-direction: column;
  }

  .coupon-card::before {
    left: -5px;
    right: -5px;
    top: 140px;
    bottom: auto;
    width: auto;
    height: 10px;
    background: radial-gradient(circle at top, transparent 5px, white 5px);
    background-size: 10px 10px;
    background-position: center top;
  }

  .coupon-left {
    width: 100%;
    padding: 20px;
  }

  .coupon-right {
    flex-direction: column;
    align-items: flex-start;
  }

  .coupon-status {
    margin-left: 0;
    margin-top: 15px;
  }
}
</style>
